﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using OpenCvSharp;
using OpenCvSharp.Blob;
using System.Windows;

namespace RecognitionNumbers
{
    public class MatrixSymbol
    {
        public List<double> matrix { get; set; }
        public int count { get; set; }
        public char symbol { get; set; }
        public double topdown { get; set; }
        public int blobs { get; set; } 

        public void clear()
        {
            count = 0;
            matrix = new List<double>();
            for (int i = 1; i <= 20; i++)
                for (int j = 1; j <= 10; j++)
                    matrix.Add(0);
        }

        public void add(IplImage img)
        {
            CvSize size = new CvSize(10, 20);
            IplImage t = new IplImage(size, img.Depth, img.NChannels);
            Cv.Resize(img, t, Interpolation.Linear);
            Gray gray = new Gray(t);
            IplImage imgLabel = new IplImage(img.Size, BitDepth.F32, 1);
            Binarization blackWhte = new Binarization(gray.output, 125);

            int  c = new CvBlobs().Count();
            blobs = Convert.ToInt32( (blobs * count + Convert.ToDouble(c)) / (count + 1));

            int top = 0;
            int down = 0;
            for (int i = 0; i < blackWhte.output.Height; i++)
                for (int j = 0; j < blackWhte.output.Width; j++)
                {
                    if (blackWhte.output[i, j].Val0 == 0)
                    {
                        matrix[(i) * blackWhte.output.Width + j] = (matrix[(i) * blackWhte.output.Width + j] * count + 1) / Convert.ToDouble(count + 1);
                        if (i < blackWhte.output.Height / 2)
                            top++;
                        else
                            down++;
                    }
                    else
                    {
                        matrix[(i) * blackWhte.output.Width + j] = (matrix[(i) * blackWhte.output.Width + j] * count) / Convert.ToDouble(count + 1);
                    }
                }
            topdown = (topdown * count + Convert.ToDouble(top) / down) / (count + 1);
            count++;
            
        }

        

        public double compare(IplImage img)
        {
            
            CvSize size = new CvSize(10, 20);
            IplImage t = new IplImage(size, img.Depth, img.NChannels);
            Cv.Resize(img, t, Interpolation.Linear);
            Gray gray = new Gray(t);
            Binarization blackWhite = new Binarization(gray.output, 125);
            int c = new CvBlobs().Count();
            blobs = Convert.ToInt32((blobs * count + Convert.ToDouble(c)) / (count + 1));
            int top = 0;
            int down = 0;
     
            double sum = 0;
            for (int i = 0; i < blackWhite.output.Height; i++)
                for (int j = 0; j < blackWhite.output.Width; j++)
                {
                    if (blackWhite.output[i, j].Val0 == 0)
                    {
                        sum += 1.0 - matrix[(i) * blackWhite.output.Width + j];
                        if (i < blackWhite.output.Height / 2)
                            top++;
                        else
                            down++;
                    }
                    else
                    {
                        sum += matrix[(i) * blackWhite.output.Width + j];
                    }

                }

            double k = 1+Math.Abs(c-blobs);

            if ((((Convert.ToDouble(top) / down) - blobs) > 0.4) | (((Convert.ToDouble(top) / down) - blobs) < -0.4))
                k++;
            return sum*k;

        }
    }
}
